package com.ltmall.api.comm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ltmall.api.comm.json.City;
import com.ltmall.api.entity.JsonCity;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class CityDataParse {

    private static List<City> cityList = new ArrayList<>();

    // 注意filepath的内容；
    static String filePath = Path.disk + Path.project + File.separator + "resource"
            + File.separator + "cityData.json";
    // 注意filepath的内容；
    static String constantPath = Path.disk + Path.project + File.separator + "resource"
            + File.separator + "cityData.json";

    public static String readFile() {
        BufferedReader reader = null;
        String laststr = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(filePath);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }

    public static void initJsonPath(String path) {
        String certain = "resource" + File.separator + "cityData.json";
        filePath = path + certain;
    }

    public static void excute() {
        List<City> tempCityList = new ArrayList<>();
        String jsonContext = readFile();
        JSONArray jsonArray = JSONArray.parseArray(jsonContext);

        int size = jsonArray.size();
        System.out.println("Size: " + size);

        for (int i = 0; i < size; i++) {

            City c = new City();

            JSONObject jObject = jsonArray.getJSONObject(i);
            String section = jObject.getString("n");
            JSONArray array = jObject.getJSONArray("s");

            c.setA(section);

            List<Map<String, List<String>>> B = new ArrayList<>();

            for (int j = 0; j < array.size(); j++) {

                Map<String, List<String>> m = new HashMap<>();
                JSONObject obj1 = array.getJSONObject(j);
                String section1 = obj1.getString("n");
                JSONArray arr = obj1.getJSONArray("s");
                List<String> list = new ArrayList<>();
                if (arr != null) {
                    for (int k = 0; k < arr.size(); k++) {
                        JSONObject obj2 = arr.getJSONObject(k);
                        String bj = obj2.getString("n");
                        list.add(bj);
                    }
                } else {
                    list = null;
                }

                m.put(section1, list);

                B.add(m);
            }
            c.setB(B);
            tempCityList.add(c);
        }

        cityList = tempCityList;
    }

    public static String getProvince(String address) {
        if (cityList.size() == 0) {
            excute();
        }

        for (City city : cityList) {
            String province = city.getA();
            List<Map<String, List<String>>> cityList = city.getB();
            for (Map<String, List<String>> map : cityList) {
                Iterator<Entry<String, List<String>>> it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, List<String>> entry = it.next();
                    String c = entry.getKey();
                    if (c.equals(address)) {
                        return province;
                    }
                }
            }
        }
        return null;
    }

    public static String getCity(String county) {
        if (cityList.size() == 0) {
            excute();
        }

        for (City city : cityList) {
            List<Map<String, List<String>>> cityList = city.getB();
            for (Map<String, List<String>> map : cityList) {

                Iterator<Entry<String, List<String>>> it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, List<String>> entry = it.next();

                    String c = entry.getKey();
                    List<String> countyList = entry.getValue();

                    if (countyList == null) {
                        continue;
                    }

                    for (String c1 : countyList) {
                        if (c1.equals(county)) {
                            return c;
                        }
                    }

                }
            }
        }
        return null;
    }

    /**
     * 获取一个市下面所有的县
     *
     * @param city city
     * @param temp temp
     * @return List<JsonCity>
     */
    public static List<JsonCity> getCounty(String city, String temp) {
        if (cityList.size() == 0) {
            excute();
        }
        List<JsonCity> cList = new ArrayList<>();

        for (City cy : cityList) {
            // 省份
            String province = cy.getA();
            List<Map<String, List<String>>> cityList = cy.getB();
            for (Map<String, List<String>> map : cityList) {
                Iterator<Entry<String, List<String>>> it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, List<String>> entry = it.next();
                    // 市
                    String c = entry.getKey();
                    List<String> countyList = entry.getValue();

                    // 市相同
                    if (city.equals(c)) {
                        if (countyList != null) {
                            for (String county : countyList) {
                                if (!county.equals(temp)) {
                                    JsonCity jc = new JsonCity();
                                    jc.setProvince(province);
                                    jc.setCity(c);
                                    jc.setCounty(county);
                                    cList.add(jc);
                                }
                            }

                            return cList;
                        }
                    }
                }
            }
        }

        return null;
    }

    /**
     * 获取一个省下面所有的市
     *
     * @param province province
     * @param temp     temp
     * @return List<JsonCity>
     */
    public static List<JsonCity> getCitys(String province, String temp) {
        if (cityList.size() == 0) {
            excute();
        }

        List<JsonCity> cList = new ArrayList<>();
        for (City cy : cityList) {
            String pro = cy.getA();
            // 省份
            if (pro.equals(province)) {
                List<Map<String, List<String>>> cityList = cy.getB();
                for (Map<String, List<String>> map : cityList) {
                    Iterator<String> it = map.keySet().iterator();
                    if (it.hasNext()) {
                        String c = it.next();
                        if (!c.equals(temp)) {
                            JsonCity jc = new JsonCity();
                            jc.setProvince(pro);
                            jc.setCity(c);
                            cList.add(jc);
                        }
                    }
                }

                return cList;
            }
        }
        return null;
    }
}